import { _decorator, Component, Button, input, Input, KeyCode } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('ButtonSimulateKey')
export class ButtonSimulateKey extends Component {
    @property(Button)
    upButton: Button | null = null;

    @property(Button)
    downButton: Button | null = null;

    @property(Button)
    leftButton: Button | null = null;

    @property(Button)
    rightButton: Button | null = null;

    onLoad() {
        // 鈕綁定點擊事件
        if (this.upButton) {
            this.upButton.node.on(Button.EventType.CLICK, this.onUpKeyPress, this);
        }
        if (this.downButton) {
            this.downButton.node.on(Button.EventType.CLICK, this.onDownKeyPress, this);
        }
        if (this.leftButton) {
            this.leftButton.node.on(Button.EventType.CLICK, this.onLeftKeyPress, this);
        }
        if (this.rightButton) {
            this.rightButton.node.on(Button.EventType.CLICK, this.onRightKeyPress, this);
        }
    }

    // 方向鍵按下邏輯
    onUpKeyPress() {
        this.simulateKeyPress(KeyCode.ARROW_UP);
    }

    onDownKeyPress() {
        this.simulateKeyPress(KeyCode.ARROW_DOWN);
    }

    onLeftKeyPress() {
        this.simulateKeyPress(KeyCode.ARROW_LEFT);
    }

    onRightKeyPress() {
        this.simulateKeyPress(KeyCode.ARROW_RIGHT);
    }
 
    simulateKeyPress(keyCode: KeyCode) {
        // 模擬鍵盤按鍵按下的動作
        this.handleKeyDown(keyCode);
    }

    handleKeyDown(keyCode: KeyCode) {
        // 根據按鍵處理相應邏輯
        switch (keyCode) {
            case KeyCode.ARROW_UP:
                console.log("Up Key Pressed");
                break;
            case KeyCode.ARROW_DOWN:
                console.log("Down Key Pressed");
                break; 
            case KeyCode.ARROW_LEFT:
                console.log("Left Key Pressed");
                break;
            case KeyCode.ARROW_RIGHT:
                console.log("Right Key Pressed");
                break;
        }
    }
}
